package objects;

import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;

import common.RealmConf;
import common.RealmServer;

public class RealmServerThread implements Runnable
{
	private boolean _running = false;
	private static ArrayList<RealmThread> _clients = new ArrayList<RealmThread>();
	
	public void run()
	{
		try {
			ServerSocket ss = new ServerSocket(RealmConf._realmPort);
			_running = true;
			RealmServer.writeLine("Ok!");
			while(_running)//block on ss.accept()
			{
				try
				{
					_clients.add(new RealmThread(ss.accept()));
				}catch(Exception e)
				{
					RealmServer.writeLine("Error: "+e.getMessage());
					e.printStackTrace();
					RealmServer.writeLine("Server is now exiting...");
					System.exit(1);
				}
			}
		} catch (IOException e)
		{
			RealmServer.writeLine("Error: Realm-server socket cannot be open");
			RealmServer.writeLine("Maybe the port is not available...");
			RealmServer.writeLine("Server is now exiting...");
			System.exit(1);
			return;
		}
	}

	public static ArrayList<RealmThread> get_clients()
	{
		return _clients;
	}
	
}
